/**
 * Copyright &copy; com.nmf
 */
package com.nmf.pc.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nmf.dao.gen.bean.BizCompany;
import com.nmf.dao.gen.bean.BizStudent;
import com.nmf.pc.enums.LoginType;
import com.nmf.pc.enums.RspStatus;
import com.nmf.pc.service.PcLoginService;
import com.nmf.pc.vo.MemberLoginCodeReq;
import com.nmf.pc.vo.MemberLoginReq;
import com.nmf.pc.vo.RestPswCodeReq;
import com.nmf.pc.vo.ReturnResult;
import com.nmf.site.biz.comp.service.BizCompanyService;
import com.nmf.site.biz.member.gen.service.BizMemberService;
import com.nmf.site.biz.sms.bx.service.SmsCodeService;
import com.nmf.site.biz.student.service.BizStudentService;
import com.nmf.site.common.MD5PawService;
import com.nmf.site.common.enums.DelFlagEnums;
import com.nmf.site.common.web.BaseController;
import com.nmf.site.modules.sys.service.SystemService;
import com.taobao.api.ApiException;

/**
 * 订单Controller
 * @author 北冥
 * @version 2018-02-10
 */
@Controller
@RequestMapping(value = "/pc/p")
public class PcLoginController extends BaseController {
	@Autowired
	BizMemberService bizMemberService;
	@Autowired
	SmsCodeService smsCodeService;
	@Autowired
	BizStudentService bizStudentService;
	@Autowired
	BizCompanyService bizCompanyService;
	@Autowired
	PcLoginService pcLoginService;
	@RequestMapping(value = {"mem/loginPsw"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult loginPsw(@Valid MemberLoginReq memberLoginReq, BindingResult result,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		
		LoginType loginType = LoginType.getStatus(memberLoginReq.getLoginType());
		if(LoginType.student.equals(loginType)) {
			BizStudent bizStudent = bizStudentService.findByTelno(memberLoginReq.getTelNo());
			
			if(bizStudent==null) {
				returnResult.setStatus(RspStatus.FAILED.value);
				returnResult.setMessage("该用户不存在，请注册");
			}else {
				PcLoginService.login(memberLoginReq.getPassword(), bizStudent.getPassword(), bizStudent.getDelFlag(), memberLoginReq.getTelNo(), returnResult);
			}
		}else if(LoginType.company.equals(loginType)) {
			BizCompany bizCompany = bizCompanyService.findByTelno(memberLoginReq.getTelNo());
			
			if(bizCompany==null) {
				returnResult.setStatus(RspStatus.FAILED.value);
				returnResult.setMessage("该用户不存在，请注册");
			}else {
				PcLoginService.login(memberLoginReq.getPassword(), bizCompany.getPassword(), 0, memberLoginReq.getTelNo(), returnResult);
			}
		}
		
		
		return returnResult;
	}
	
	@RequestMapping(value = {"mem/loginCode"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult login(@Valid MemberLoginCodeReq memberLoginCodeReq, BindingResult result, HttpServletRequest request, HttpServletResponse response, Model model) {
		ReturnResult returnResult = new ReturnResult();
		
		LoginType loginType = LoginType.getStatus(memberLoginCodeReq.getLoginType());
		
		DelFlagEnums delFlagem = DelFlagEnums.DEL_FLAG_NORMAL;
		
		if(LoginType.student.equals(loginType)) {
			BizStudent bizStudent = bizStudentService.findByTelno(memberLoginCodeReq.getTelNo());
			delFlagem = DelFlagEnums.getType(bizStudent.getDelFlag());
		}else if(LoginType.company.equals(loginType)) {
			BizCompany bizCompany = bizCompanyService.findByTelno(memberLoginCodeReq.getTelNo());
		}
		
		pcLoginService.loginByCode(memberLoginCodeReq.getCode(), memberLoginCodeReq.getTelNo(), delFlagem.value, returnResult);

		return returnResult;
	}
	
	@RequestMapping(value = {"mem/resetPsw"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult resetPsw(@Valid RestPswCodeReq restPswCodeReq, BindingResult result, HttpServletRequest request, HttpServletResponse response, Model model) {
		ReturnResult returnResult = new ReturnResult();
		
		String enPassword = SystemService.entryptPassword(restPswCodeReq.getPsw());
		if(true) {
			LoginType loginType = LoginType.getStatus(restPswCodeReq.getLoginType());
			boolean isExt = true;
			if(LoginType.student.equals(loginType)) {
				BizStudent bizStudent = bizStudentService.findByTelno(restPswCodeReq.getTelNo());
				if(bizStudent==null) {
					isExt = false;
				}else {
					BizStudent bizStudentRecord = new BizStudent();
					bizStudentRecord.setPassword(enPassword);
					bizStudentRecord.setId(bizStudent.getId());
					bizStudentService.save(bizStudentRecord);
				}
			}else if(LoginType.company.equals(loginType)) {
				BizCompany bizCompany = bizCompanyService.findByTelno(restPswCodeReq.getTelNo());
				if(bizCompany==null) {
					isExt = false;
				}else {
					BizCompany bizCompanyRecord = new  BizCompany();
					bizCompanyRecord.setPassword(enPassword);
					bizCompanyRecord.setId(bizCompany.getId());
					bizCompanyService.save(bizCompanyRecord);
				}
			}
			if(!isExt) {
				returnResult.setStatus(RspStatus.FAILED.value);
				returnResult.setMessage("该用户不存在，请注册");
			}
			returnResult.setData("token", MD5PawService.getMd5Psw(restPswCodeReq.getTelNo()));
		}else {
			returnResult.setStatus(RspStatus.FAILED.value);
			returnResult.setMessage("验证码错误，请重新获取");
		}
		

		return returnResult;
	}
	
	@RequestMapping(value = {"mem/veryMobileCode"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult getMobileCode(@RequestParam(required=true) String telNo,@RequestParam(required=true) String veryCode, HttpServletRequest request, HttpServletResponse response) throws ApiException {
		ReturnResult returnResult = new ReturnResult();
//		smsCodeService.isRight(returnResult,telNo, veryCode);
		return returnResult;
	}

}